<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fetch</title>
</head>
<body>
    <script>
        /**
         * Fetch 时一个基于 Promise 的 api，可以发送http请求，返回的是一个 Promise 对象。
         */

       let getUrl = "http://127.0.0.1:9090/ss-app/sc/get";
       let postUrl = "http://127.0.0.1:9090/ss-app/sc/post/form";
       let postJsonUrl = "http://127.0.0.1:9090/ss-app/sc/post/body";
       // get
       fetch(getUrl).then(response => {
            console.log("response", response);
            return response.json();
       }).then(data => {
            console.log("get.data: ", data);
       }).catch(e => {
            console.log("get.error: ", e.message);
       }).finally(() => {
            console.log("get.finally");
       });

       // post form
       fetch(postUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: new URLSearchParams({
                "annexId": "1046878434611077121",
                "bizId": "1046878434611077121"
            })
       }).then(response => {
            console.log("response", response);
            return response.json();
       }).then(data => {
            console.log("post.data: ", data);
       }).catch(e => {
            console.log("post.error: ", e.message);
       }).finally(() => {
            console.log("post.finally");
       });

       // post json
       fetch(postJsonUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                "loginName": "Joy",
                "password": "******"
            })
       }).then(response => {
            console.log("response", response);
            return response.json();
       }).then(data => {
            console.log("post.data: ", data);
       }).catch(e => {
            console.log("post.error: ", e.message);
       }).finally(() => {
            console.log("post.finally");
       });
    </script>
</body>
</html>